#include<iostream>
#include <iostream>
#include <fstream>
#include <algorithm>
#define length 8

using namespace std;

int sum(int a, int b, int c, int d){
	return a+b+c+d;
}

bool isEqual(int * data1){
	return (sum(data1[0],data1[1],data1[2],data1[3]) == sum(data1[4],data1[5],data1[6],data1[7]))
		&& (sum(data1[2],data1[3],data1[6] ,data1[7]) == sum(data1[0],data1[1],data1[4],data1[5]))
		&& (sum(data1[0],data1[2],data1[4] ,data1[6]) == sum(data1[1],data1[3],data1[5],data1[7]));
}

void swap(int *data1, int * data2){
	int tmp = *data1;
	*data1 = *data2;
	*data2 = tmp;
}
bool permutation(int * data1, int* data2 ){
	bool result = false;
	// 方法：先固定一个字符，再寻找后面所有字符的排序
	if ( data2 == (data1+length-1)){
		if ( isEqual(data1) ){
			result = true;
		}
	}else {
		for ( int i = 0; i<length; i++ ){
			// 交换字符
			swap(data2+i, data2);
			result = permutation(data1, data2+1);
			if ( result )
				break;
			// 再将交换的字符交换到原来的字符
			swap(data2+i, data2);
		}
	}
	return result;
}

void permutation(int * data){
	if ( data == nullptr ){
		return;
	}

	bool result = permutation(data, data);
	if ( result ){
		cout << "find nums : " << endl;
		for ( int i = 0; i< length; i++ ){
			cout << data[i] << "  ";
		}
		cout << endl;
	}else {
		cout << "not find nums " << endl;
	}
}

void Test(int * data)
{
    if(data == nullptr)
        return;

    permutation(data);
    cout << "---------------" << endl;
}


int main(void){
    int int1[length] = {1,2,3,1,2,3,2,2};
	int int2[length] = {1,2,3,1,8,3,2,2};
	Test(int1);
	Test(int2);
    return 0;
}